package com.zhao.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.Result;
import com.zhao.entity.To.CourseTo;
import com.zhao.entity.Vo.CourseVo;
import com.zhao.entity.Vo.TeacherQuery;
import com.zhao.service.EduCourseService;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaozhao
 * @since 2021-07-26
 */
@RestController
@RequestMapping("eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //添加课程的基本信息
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody CourseVo courseVo){
        if(courseVo!=null){
            String id=courseService.addCourse(courseVo);
            //返回课程信息的主键id
            return Result.ok().data("id",id);
        }
        else {
            return Result.error().message("courseVo数据为空");
        }
    }
    //根据id查询课程的基本信息
    @GetMapping("/getAllList/{id}")
    public Result getAllList(@PathVariable String id){
        CourseVo course=courseService.findAllInfo(id);
        return Result.ok().data("course",course);
    }
    //根据id修改课程的基本信息
    @PostMapping("/updateCourse")
    public Result updateCourse(@RequestBody CourseVo courseVo){
        String id=courseService.updateCourse(courseVo);
        return Result.ok().data("id",id);
    }

    //根据课程id查询所有的信息
    @GetMapping("findAll/{id}")
    public Result findAll(@PathVariable String id){
       CourseTo courseTo= courseService.getAll(id);
        return Result.ok().data("courseTo",courseTo);
    }

    //发布课程
    @PostMapping("publicCourse/{id}")
    public Result publicCourse(@PathVariable String id){
        int i= courseService.publicCourseById(id);
       return Result.ok();
    }

    //查询已经发布的课程信息
    @PostMapping("getPublicCourse/{currentPage}/{pageSize}")
    public Result getPublicCourse(@PathVariable("pageSize")Integer pageSize,
                                  @PathVariable("currentPage")Integer currentPage,
                                  @RequestBody TeacherQuery teacherQuery){

        Page page = courseService.findAllCourseInfo(teacherQuery, pageSize, currentPage);
        long total = page.getTotal();
        List records = page.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }
    //根据课程id修改课程发布的状态
    @PostMapping("/changStatusByCourseId/{id}/{status}")
    public Result changStatusByCourseId(@PathVariable("id")String id,@PathVariable("status")String status){
        courseService.changeStatus(id,status);
        return Result.ok();
    }

    //根据课程id删除已发布课程信息
    @PostMapping("/delByCourseId/{id}")
    public Result delByCourseId(@PathVariable String id){
       int i= courseService.delPublicCourseById(id);
       if(i>0){
           return Result.ok();
       } else {
           return Result.error();
       }
    }
}

